package middleware

import (
	"time"

	"gitee.com/botaoshow/yuanxin/web/y"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

func LoggerHandler(logger *logrus.Logger) gin.HandlerFunc {
	return func(c *gin.Context) {
		start := time.Now()

		// Record request information
		y.WithFields(logrus.Fields{
			"method":     c.Request.Method,
			"path":       c.Request.URL.Path,
			"client_ip":  c.ClientIP(),
			"user_agent": c.Request.UserAgent(),
		}).Info("Request started")

		// Process request
		c.Next()

		// Record response information
		duration := time.Since(start)
		y.WithFields(logrus.Fields{
			"method":    c.Request.Method,
			"path":      c.Request.URL.Path,
			"status":    c.Writer.Status(),
			"client_ip": c.ClientIP(),
			"duration":  duration,
			"size":      c.Writer.Size(),
		}).Info("Request completed")
	}
}
